import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { LocalStorage } from '@core/local.storage';
import { environment } from '../../../../environments/environment';
import { HttpHeaders } from '@angular/common/http';
import { forkJoin } from 'rxjs';
import { NzMessageService } from 'ng-zorro-antd';
const responseHeaders = new HttpHeaders().set('Content-Type', 'application/json;charset=UTF-8');
@Component({
  selector: 'app-zhpdsz-day',
  templateUrl: './zhpdsz.component.html',
  styleUrls: ['./zhpdsz.less']
})
export class ZhpdszComponent implements OnInit {

  userInfo = JSON.parse(this.ls.get('loginInfo'));


  tablestyle = {
    nzScroll1: {
      y: `calc(100vh - 350px)`,
      x: '1600px'
    },
    nzScroll2: {
      y: `calc(100vh - 350px)`,
      x: '1500px'
    }
  };

  zjbjInfo: any[] = []; // 专家报警
  zjyjInfo: any[] = []; // 专家预警
  jcbjInfo: any[] = []; // 监测报警
  info: any[] = [];


  constructor(
    private http: _HttpClient,
    public ls: LocalStorage,
    public msg: NzMessageService
  ) { }



  ngOnInit(): void {
    this.initCmp();
    this.query();
  }

  initCmp() {

  }
  /**
     * 删除私立字段
     * @param param
     */
  removePrivateFields(param) {
    Object.keys(param).forEach(key => {
      if (key.indexOf('_') == 0) {
        delete param[key];
      }
    });
  }


  query() {
    this.getInfo().subscribe(res => {
      this.info = res.data;
      const info = JSON.parse(JSON.stringify(res.data));
      this.zjbjInfo = [];
      this.zjyjInfo = [];
      this.jcbjInfo = [];
      info.forEach(row => {
        switch (row.type) {
          case 'expertsystem$alarm':
            this.zjbjInfo.push(row);
            break;
          case 'expertsystem$prealarm':
            this.zjyjInfo.push(row);
            break;
          case 'alarmtable':
            this.jcbjInfo.push(row);
            break;

          default:
            break;
        }
      });
    });
  }

  getInfo() {
    const dataurl = environment.problem + '/alarmlevelScore/query';
    return this.http.get(dataurl);
  }

  saveOne(params) {
    const dataurl = environment.problem + '/alarmlevelScore/update';
    return this.http.post(dataurl, params, {}, { headers: responseHeaders });
  }

  getDiff(arr1, arr2) {
    const diff = [];
    arr1.forEach(el => {
      const row2 = arr2.find(el2 => el2.id == el.id);
      if (el.score != row2.score) {
        diff.push(el);
      }
    });
    return diff;
  }

  saveAll() {
    const diff = this.getDiff([...this.zjbjInfo, ...this.zjyjInfo, ...this.jcbjInfo], this.info);

    const obs = diff.map(el => {
      return this.saveOne({ id: el.id, score: el.score });
    });
    forkJoin(obs).subscribe(resArr => {
      let res = true;
      resArr.forEach(_res => {
        if (_res.msg != 'Ok') {
          res = false;
        }
      });
      if (res) {
        this.msg.success('保存成功');
        this.query();
      } else {
        this.msg.success('保存失败');
      }
    });
  }
}
